<?php
namespace app\atxunda\controller;
use think\Controller;
use think\Db; 
use think\Request;

class Admin extends Common
{
	/**
	 * [pass 修改密码]
	 * @return [type] [description]
	 */
	public function pass(){
		$admin = $this->admin;
		if(input('post.')){
			$data = Db::table('admin')->where('admin_id',$admin['admin_id'])->find();
			if($data['admin_password'] != md5(input('post.mpass'))){
				$this->error('原始密码错误');
			}else if(input('post.newpass') != input('post.renewpass')){
				$this->error('新密码跟确认密码不一致');
			}else{
				if(Db::table('admin')->where('admin_id',$admin['admin_id'])->update(['admin_password'=>md5(input('post.newpass'))])){
					$this->success('修改成功，请重新登录','login/login');
				}else{
					$this->error('修改密码失败');
				}
			}

		}else{
			$this->assign('admin_name',$admin['admin_name']);
			return $this->fetch('pass');
		}
	}

	/**
	 * [index 管理员管理]
	 * @return [type] [description]
	 */
	public function index(){
		$role_data = Db::table('role')->select();
		$this->assign('role_data',$role_data);
		$nav_data = Db::table('nav')->where('is_show',1)->select();
		$this->assign('nav_data',$nav_data);
		$data = Db::table('admin')
				->field("admin_id,GROUP_CONCAT(role_name) as role_name,admin_name,GROUP_CONCAT(nav_name) as nav_name,phone")
				->where(['admin_id'=>['neq','1'],'status'=>'1','hide'=>'no'])
				->join('role','FIND_IN_SET(role.role_id,admin.role_id)','left')
				->join('nav','FIND_IN_SET(nav.nav_id,admin.nav_id)','left')
				->group('admin_id')
				->select();
		/*foreach ($data as $key => $v) {
			$arr = array();
			$navNames = Db::table('nav')->field('nav_name')->where('nav_id','in',$v['nav_id'])->select();
			foreach ($navNames as $k => $vn) {
				$arr[] = $vn['nav_name'];
			}
			$data[$key]['nav_name'] = implode(',', $arr);
		}*/
		$this->assign('data',$data);
		return $this->fetch('index');
	}


	/**
	 * [add 添加管理员]
	 */
	public function add(){
		if(input('post.')){
			$data = input('post.');
			$data['admin_password'] = md5(input('post.admin_password'));
			unset($data['repassword']);
			$salt = $this->random(6);
			if($this->checksalt($salt)){
				$data['salt'] = $this->checksalt($salt);
			}
			$data['role_id'] = implode(',',$data['role_id']);
			if($data['nav_id']) $data['nav_id'] = implode(',', $data['nav_id']);
			if(Db::table('admin')->insert($data)){
				$this->success('添加成功','index','',1);
			}else{
				$this->error('添加失败');
			}
		}else{
			$nav_data = Db::table('nav')->where('is_show',1)->order('nav_sort desc')->select();
			$role_data = Db::table('role')->select();
			$this->assign('role_data',$role_data);
			$this->assign('nav_data',$nav_data);
			return $this->fetch('add');
		}
	}

	public function checksalt($salt){
		$admin_data = Db::table('admin')->where('salt',$salt)->find();
		if($admin_data){
			$salt = $this->random(6);
			$this->checksalt($salt);
		}else{
			return $salt;
		}
	}

	public function random($length, $chars = '123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ') {
		$hash = '';
		$max = strlen($chars) - 1;
		for($i = 0; $i < $length; $i++) {
			$hash .= $chars[mt_rand(0, $max)];
		}
		return $hash;
	}

	/**
	 * [edit 编辑管理员]
	 * @return [type] [description]
	 */
	public function edit(){
		$request = Request::instance();
		$admin_id = $request->param('admin_id');
		if(input('post.')){
			$admin_data = Db::table('admin')->where('admin_id',input('post.admin_id'))->find();
			if(input('post.admin_password') && (md5(input('post.admin_password')) != $admin_data['admin_password']))
				$this->error('原密码错误');
			$data = input('post.');
			if($data['nav_id']){
				$data['nav_id'] = implode(',', $data['nav_id']);
			}else{
				$data['nav_id'] = 0;
			}
			if(input('post.newpass')){
				$data['admin_password'] = md5(input('post.newpass'));
			}else{
				$data['admin_password'] = $admin_data['admin_password'];
			}
			unset($data['repassword']);
			unset($data['newpass']);
			$data['role_id'] = implode(',',$data['role_id']);
			if(Db::table('admin')->where('admin_id',$data['admin_id'])->update($data)){
				$this->success('编辑成功','index','',1);
			}else{
				$this->error('编辑失败');
			}
		}else{
			$role_data = Db::table('role')->select();
			$this->assign('role_data',$role_data);
			$admin_data = Db::table('admin')->where('admin_id',$admin_id)->find();
			$nav_data = Db::table('nav')->where('is_show',1)->order('nav_sort desc')->select();
			$arr = explode(',', $admin_data['nav_id']);
			$this->assign('arr',$arr);
			$this->assign('nav_data',$nav_data);
			$this->assign('admin_data',$admin_data);
			return $this->fetch('edit');
		}
	}


	public function delete(){
		$request = Request::instance();
		$admin_id = $request->param('admin_id');
		if(Db::table('admin')->delete($admin_id)){
            Db::table('know_user_class')->where('target',$admin_id)->update(['target'=>0]);
            Db::table('admin_group')->where('admin_id',$admin_id)->delete();
			$this->success('删除成功','index','',1);
		}else{
			$this->error('删除失败');
		}
	}

	public function rands(){
		$admin = $this->admin;
		$data = Db::table('admin')->field('salt')->where('admin_id',$admin['admin_id'])->find();
		$this->assign('salt',$data['salt']);
		return $this->fetch();
	}

	public function checkname($admin_name){
		$arr = Db::table('admin')->where(['admin_name'=>$admin_name])->find();
		$data = array();
		if($arr){
			$data['ceshi'] = 'false';
		}else{
			$data['getdata'] = 'true';
		}
		echo json_encode($data);
	}
}
